package com.beemans.common.ui.views

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.core.view.updatePadding
import com.beemans.common.R
import com.beemans.common.databinding.ViewTitleBarLayoutBinding
import com.beemans.common.ext.*
import com.tiamosu.databinding.delegate.toDataBinding
import com.tiamosu.fly.ext.clickNoRepeat

/**
 * @author tiamosu
 * @date 2021/3/15
 */
class TitleBarLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private val binding: ViewTitleBarLayoutBinding by lazy { R.layout.view_title_bar_layout.toDataBinding()!! }

    companion object {
        const val STATUS_BOTH = 0
        const val STATUS_ONLY_STATUS_BAR = 1
        const val STATUS_ONLY_TITLE_BAR = 2
    }

    init {
        addView(binding.root)

        val ta = context.obtainStyledAttributes(attrs, R.styleable.TitleBarLayout)
        val visibleStatus = ta.getInt(R.styleable.TitleBarLayout_tbl_visibleViewBar, STATUS_BOTH)

        //状态栏
        binding.titleBarViewStatusBar.also { statusBar ->
            val isVisible = visibleStatus == STATUS_BOTH || visibleStatus == STATUS_ONLY_STATUS_BAR
            statusBar.isVisible = isVisible

            if (isVisible) {
                statusBar.updateLayoutParams { height = context.getStatusBarHeight }

                if (ta.hasValue(R.styleable.TitleBarLayout_tbl_statusBarColor)) {
                    val backgroundColor =
                        ta.getColor(R.styleable.TitleBarLayout_tbl_statusBarColor, Color.WHITE)
                    statusBar.setBackgroundColor(backgroundColor)
                }
            }
        }

        //标题栏
        binding.titleBarFlTitleBar.also { titleBar ->
            val isVisible = visibleStatus == STATUS_BOTH || visibleStatus == STATUS_ONLY_TITLE_BAR
            titleBar.isVisible = isVisible

            if (isVisible) {
                if (ta.hasValue(R.styleable.TitleBarLayout_tbl_titleBarColor)) {
                    val backgroundColor =
                        ta.getColor(R.styleable.TitleBarLayout_tbl_titleBarColor, Color.WHITE)
                    titleBar.setBackgroundColor(backgroundColor)
                }

                val height =
                    ta.getDimensionPixelSize(R.styleable.TitleBarLayout_tbl_titleBarHeight, -1)
                if (height != -1) {
                    titleBar.updateLayoutParams { this.height = height }
                }
            }
        }

        if (binding.titleBarFlTitleBar.isVisible) {
            //左边按钮
            binding.titleBarIvLeft.also { ivLeft ->
                val isVisible = ta.getBoolean(R.styleable.TitleBarLayout_tbl_ivLeftVisible, true)
                ivLeft.isVisible = isVisible

                val icon = ta.getResourceId(R.styleable.TitleBarLayout_tbl_ivLeftIcon, -1)
                if (icon != -1) {
                    val imgWidth =
                        ta.getDimensionPixelSize(R.styleable.TitleBarLayout_tbl_ivLeftWidth, 0)
                    val imgHeight =
                        ta.getDimensionPixelSize(R.styleable.TitleBarLayout_tbl_ivLeftHeight, 0)
                    ivLeft.loadImage(icon) { options { override(imgWidth, imgHeight) } }
                }

                if (ta.hasValue(R.styleable.TitleBarLayout_tbl_ivLeftColor)) {
                    val color = ta.getColor(R.styleable.TitleBarLayout_tbl_ivLeftColor, Color.WHITE)
                    ivLeft.setColorFilter(color)
                }

                val marginStart =
                    ta.getDimensionPixelSize(R.styleable.TitleBarLayout_tbl_ivLeftMarginStart, -1)
                if (marginStart != -1) {
                    ivLeft.updateMarginLayoutParams { this.marginStart = marginStart }
                }

                val paddingLeft =
                    ta.getDimensionPixelSize(R.styleable.TitleBarLayout_tbl_ivLeftPaddingLeft, -1)
                if (paddingLeft != -1) {
                    ivLeft.updatePadding(left = paddingLeft)
                }

                val paddingRight =
                    ta.getDimensionPixelSize(R.styleable.TitleBarLayout_tbl_ivLeftPaddingRight, -1)
                if (paddingRight != -1) {
                    ivLeft.updatePadding(right = paddingRight)
                }
            }

            //标题文字
            binding.titleBarTvTitle.also { tvTitle ->
                val isVisible = ta.getBoolean(R.styleable.TitleBarLayout_tbl_tvTitleVisible, true)
                tvTitle.isVisible = isVisible

                if (ta.hasValue(R.styleable.TitleBarLayout_tbl_tvTitle)) {
                    val text = ta.getString(R.styleable.TitleBarLayout_tbl_tvTitle)
                    tvTitle.text = text
                }

                if (ta.hasValue(R.styleable.TitleBarLayout_tbl_tvTitleColor)) {
                    val textColor =
                        ta.getColor(R.styleable.TitleBarLayout_tbl_tvTitleColor, Color.WHITE)
                    tvTitle.setTextColor(textColor)
                }

                val textSize =
                    ta.getDimensionPixelSize(R.styleable.TitleBarLayout_tbl_tvTitleSize, -1)
                if (textSize != -1) {
                    tvTitle.updateTextSize(textSize.toFloat())
                }

                val isBold = ta.getBoolean(R.styleable.TitleBarLayout_tbl_tvTitleBold, false)
                tvTitle.updateTextBold(isBold)
            }

            //右边按钮
            binding.titleBarIvRight.also { ivRight ->
                val isVisible = ta.getBoolean(R.styleable.TitleBarLayout_tbl_ivRightVisible, true)
                ivRight.isVisible = isVisible

                val icon = ta.getResourceId(R.styleable.TitleBarLayout_tbl_ivRightIcon, -1)
                if (icon != -1) {
                    val imgWidth =
                        ta.getDimensionPixelSize(R.styleable.TitleBarLayout_tbl_ivRightWidth, 0)
                    val imgHeight =
                        ta.getDimensionPixelSize(R.styleable.TitleBarLayout_tbl_ivRightHeight, 0)
                    ivRight.loadImage(icon) { options { override(imgWidth, imgHeight) } }
                }

                if (ta.hasValue(R.styleable.TitleBarLayout_tbl_ivRightColor)) {
                    val color =
                        ta.getColor(R.styleable.TitleBarLayout_tbl_ivRightColor, Color.WHITE)
                    ivRight.setColorFilter(color)
                }

                val marginEnd =
                    ta.getDimensionPixelSize(R.styleable.TitleBarLayout_tbl_ivRightMarginEnd, -1)
                if (marginEnd != -1) {
                    ivRight.updateMarginLayoutParams { this.marginEnd = marginEnd }
                }

                val paddingLeft =
                    ta.getDimensionPixelSize(R.styleable.TitleBarLayout_tbl_ivRightPaddingLeft, -1)
                if (paddingLeft != -1) {
                    ivRight.updatePadding(left = paddingLeft)
                }

                val paddingRight =
                    ta.getDimensionPixelSize(R.styleable.TitleBarLayout_tbl_ivRightPaddingRight, -1)
                if (paddingRight != -1) {
                    ivRight.updatePadding(right = paddingRight)
                }
            }

            //右边文字
            binding.titleBarTvRight.also { tvRight ->
                val isVisible = ta.getBoolean(R.styleable.TitleBarLayout_tbl_tvRightVisible, true)
                tvRight.isVisible = isVisible

                if (ta.hasValue(R.styleable.TitleBarLayout_tbl_tvRight)) {
                    val text = ta.getString(R.styleable.TitleBarLayout_tbl_tvRight)
                    tvRight.text = text
                }

                if (ta.hasValue(R.styleable.TitleBarLayout_tbl_tvRightColor)) {
                    val textColor =
                        ta.getColor(R.styleable.TitleBarLayout_tbl_tvRightColor, Color.WHITE)
                    tvRight.setTextColor(textColor)
                }

                val textSize =
                    ta.getDimensionPixelSize(R.styleable.TitleBarLayout_tbl_tvRightSize, -1)
                if (textSize != -1) {
                    tvRight.updateTextSize(textSize.toFloat())
                }

                val isBold = ta.getBoolean(R.styleable.TitleBarLayout_tbl_tvRightBold, false)
                tvRight.updateTextBold(isBold)

                val marginEnd =
                    ta.getDimensionPixelSize(R.styleable.TitleBarLayout_tbl_tvRightMarginEnd, -1)
                if (marginEnd != -1) {
                    tvRight.updateMarginLayoutParams { this.marginEnd = marginEnd }
                }

                val paddingLeft =
                    ta.getDimensionPixelSize(R.styleable.TitleBarLayout_tbl_tvRightPaddingLeft, -1)
                if (paddingLeft != -1) {
                    tvRight.updatePadding(left = paddingLeft)
                }

                val paddingRight =
                    ta.getDimensionPixelSize(R.styleable.TitleBarLayout_tbl_tvRightPaddingRight, -1)
                if (paddingRight != -1) {
                    tvRight.updatePadding(right = paddingRight)
                }
            }
        }

        //是否显示下划线
        binding.titleBarViewLineBot.also { lineBot ->
            val isVisible = ta.getBoolean(R.styleable.TitleBarLayout_tbl_lineBotVisible, false)
            lineBot.isVisible = isVisible

            val backgroundColor =
                ta.getColor(
                    R.styleable.TitleBarLayout_tbl_lineBotColor,
                    R.color.color_f2f2f2.toColor()
                )
            lineBot.setBackgroundColor(backgroundColor)

            val height =
                ta.getDimensionPixelSize(R.styleable.TitleBarLayout_tbl_lineBotHeight, 1.pt2px)
            lineBot.updateLayoutParams { this.height = height }
        }

        ta.recycle()
    }

    fun setIvLeft(block: AppCompatImageView.() -> Unit) {
        binding.titleBarIvLeft.apply(block)
    }

    fun setTvTitle(block: AppCompatTextView.() -> Unit) {
        binding.titleBarTvTitle.apply(block)
    }

    fun setIvRight(block: AppCompatImageView.() -> Unit) {
        binding.titleBarIvRight.apply(block)
    }

    fun setTvRight(block: AppCompatTextView.() -> Unit) {
        binding.titleBarTvRight.apply(block)
    }

    fun setIvLeftClick(onClick: (view: View) -> Unit) {
        binding.titleBarIvLeft.clickNoRepeat { onClick.invoke(it) }
    }

    fun setIvRightClick(onClick: (view: View) -> Unit) {
        binding.titleBarIvRight.clickNoRepeat { onClick.invoke(it) }
    }

    fun setTvRightClick(onClick: (view: View) -> Unit) {
        binding.titleBarTvRight.clickNoRepeat { onClick.invoke(it) }
    }
}